home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 24 / CU Amiga Magazine's Super CD-ROM 24 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-07].iso / CUCD / Magazine / C_Tutorial / Part-9 / wb3 / main.c < prev    next >
C/C++ Source or Header  |  1998-02-01  |  6KB  |  264 lines

  1. #include<dos/dos.h>
  2. #include<dos/rdargs.h>
  3. #include<exec/libraries.h>
  4. #include<workbench/startup.h>
  5. #include<workbench/workbench.h>
  6.  
  7. #include<stdio.h>
  8. #include<stdlib.h>
  9.  
  10. #include<clib/dos_protos.h>
  11. #include<clib/exec_protos.h>
  12. #include<clib/icon_protos.h>
  13.  
  14. #include "main.h"
  15. #include "gui.h"
  16. #include "idcmp.h"
  17. #include "loadsave.h"
  18. #include "arexx.h"
  19.  
  20. /* The library base global variables */
  21. /* (The different style of opening libraries requires these to be initialised to NULL) */
  22. struct Library* GfxBase = NULL;
  23. struct Library* IntuitionBase = NULL;
  24. struct Library* GadToolsBase = NULL;
  25. struct Library* AslBase = NULL;
  26. struct Library* DosBase = NULL;
  27. struct Library* IFFBase = NULL;
  28. struct Library* RexxSysBase = NULL;
  29. struct Library* IconBase = NULL;
  30.  
  31. /* Need to give prototypes for our functions */
  32. static int  createAll(struct WBStartup*);
  33. static void freeAll(void);
  34. static int  openLibs(void);
  35. static void closeLibs(void);
  36.  
  37. /* The alternative "main()" for Workbench startup */
  38. void wbmain(struct WBStartup*);
  39.  
  40. /* The shared starting point of our program */
  41. static void realmain(struct WBStartup*);
  42.  
  43. #define TT_DEPTH       "DEPTH"
  44. #define TT_PORTNAME    "PORTNAME"
  45.  
  46. #define ARGS_TEMPLATE  "FILE," TT_DEPTH "/N," TT_PORTNAME
  47.  
  48. enum ARGS { ARG_FILENAME, ARG_DEPTH, ARG_PORTNAME, NUM_ARGS };
  49.  
  50. #define DEFAULT_PORTNAME "HELLOPAINTER"
  51. #define DEFAULT_DEPTH    (4)
  52.  
  53. /* A place to store our program name for the default tool */
  54. static char progname[MAXFILENAME];
  55.  
  56. char* progName()
  57. {
  58.     return progname;
  59. }
  60.  
  61. static void setProgName(BPTR dir, char* file)
  62. {
  63.     *progname = '\0';
  64.     NameFromLock(dir, progname, MAXFILENAME);
  65.     AddPart(progname, file, MAXFILENAME);
  66. }
  67.  
  68. /* The CLI starting point for StormC, but the general start for SAS/C */
  69. void main(int argc, char** argv)
  70. {
  71.     /* argc should never be zero: SAS/C uses this to indicate WB start */
  72.   if(argc == 0)
  73.         wbmain((struct WBStartup*)argv);
  74.     else
  75.     {
  76.         BPTR dir = Lock("PROGDIR:", ACCESS_READ);
  77.         setProgName(dir, argv[0]);
  78.         if(dir)
  79.             UnLock(dir);
  80.         realmain(NULL);
  81.     }
  82. }
  83.  
  84. /* The WB starting point for StormC */
  85. void wbmain(struct WBStartup* wbmsg)
  86. {
  87.     /* WB-specific startup could go here */
  88.     setProgName(wbmsg->sm_ArgList[0].wa_Lock, wbmsg->sm_ArgList[0].wa_Name);
  89.     realmain(wbmsg);
  90. }
  91.  
  92. /* The start of the program */
  93. static void realmain(struct WBStartup* wbmsg)
  94. {
  95.     if(createAll(wbmsg))
  96.         handleIDCMP();
  97.     freeAll();
  98. }
  99.  
  100. static struct RDArgs* rdargs = NULL;
  101. static struct DiskObject* dobj = NULL;
  102.  
  103. static int createAll(struct WBStartup* wbmsg)
  104. {
  105.     int success = FALSE;
  106.   if(openLibs())
  107.     {
  108.         /* We'll only set portname if successful */
  109.         char* portname = NULL;
  110.         UBYTE depth;
  111.         char* filename = NULL;
  112.         BPTR currdir = NULL;
  113.         if(wbmsg)
  114.         {
  115.             /* WB bit, use Tool Types */
  116.             currdir = CurrentDir(wbmsg->sm_ArgList[0].wa_Lock);
  117.             if(dobj = GetDiskObject(wbmsg->sm_ArgList[0].wa_Name))
  118.             {
  119.                 UBYTE** tt = (UBYTE**)dobj->do_ToolTypes;
  120.                 char* depthptr = FindToolType(tt, TT_DEPTH);
  121.                 portname = FindToolType(tt, TT_PORTNAME);
  122.                 /* Use the default if a Tool Type was not specified */
  123.                 if(portname == NULL)
  124.                     portname = DEFAULT_PORTNAME;
  125.                 if(depthptr)
  126.                     depth = atoi(depthptr);
  127.                 else
  128.                     depth = DEFAULT_DEPTH;
  129.             }
  130.             else
  131.                 printf("Error: could not read Tool Types\n");
  132.             /* Reset current directory, if we moved it */
  133.             if(currdir)
  134.             {
  135.                 CurrentDir(currdir);
  136.                 currdir = NULL;
  137.             }
  138.             if(wbmsg->sm_NumArgs > 1)
  139.             {
  140.                 currdir = CurrentDir(wbmsg->sm_ArgList[1].wa_Lock);
  141.                 filename = wbmsg->sm_ArgList[1].wa_Name;
  142.             }
  143.         }
  144.         else
  145.         {
  146.             /* CLI bit, use ReadArgs() */
  147.             LONG args[NUM_ARGS];
  148.             int i;
  149.             /* Initialise our args to NULL */
  150.             /* (This way we will know if an argument was specified) */
  151.             for(i=0; i<NUM_ARGS; i++)
  152.                 args[i] = NULL;
  153.             if(rdargs = ReadArgs(ARGS_TEMPLATE, args, NULL))
  154.             {
  155.                 LONG* depthptr = (LONG*)(args[ARG_DEPTH]);
  156.                 portname = (char*)(args[ARG_PORTNAME]);
  157.                 filename = (char*)(args[ARG_FILENAME]);
  158.                 /* Use the default if an argument was not specified */
  159.                 if(portname == NULL)
  160.                     portname = DEFAULT_PORTNAME;
  161.                 if(depthptr == NULL)
  162.                     depth = DEFAULT_DEPTH;
  163.                 else
  164.                     depth = (UBYTE)(*depthptr);
  165.             }
  166.             else
  167.                 printf("Error: could not read arguments\n");
  168.         }
  169.         if(portname)
  170.         {
  171.             if(createARexxPort(portname) && createArgs() && openGUI(depth,0,0,0))
  172.             {
  173.                 success = TRUE;
  174.                 if(filename)
  175.                     loadfile(filename);
  176.             }
  177.         }
  178.         /* Reset current directory, if we moved it */
  179.         if(currdir)
  180.             CurrentDir(currdir);
  181.     }
  182.     return success;
  183. }
  184.  
  185. static void freeAll()
  186. {
  187.     freeReqs();
  188.     closeGUI();
  189.     freeArgs();
  190.     freeARexxPort();
  191.     if(rdargs)
  192.         FreeArgs(rdargs);
  193.     if(dobj)
  194.         FreeDiskObject(dobj);
  195.     closeLibs();
  196. }
  197.  
  198. /* Try to open all the libraries -- return TRUE on success */
  199. static int openLibs()
  200. {
  201.     if((GfxBase = OpenLibrary("graphics.library",37)) == NULL)
  202.     {
  203.         printf("Error: could not open graphics.library\n");
  204.         return FALSE;
  205.     }
  206.     if((IntuitionBase = OpenLibrary("intuition.library",37)) == NULL)
  207.     {
  208.         printf("Error: could not open intuition.library\n");
  209.         return FALSE;
  210.     }
  211.     if((GadToolsBase = OpenLibrary("gadtools.library",37)) == NULL)
  212.     {
  213.         printf("Error: could not open gadtools.library\n");
  214.         return FALSE;
  215.     }
  216.     if((AslBase = OpenLibrary("asl.library",37)) == NULL)
  217.     {
  218.         printf("Error: could not open asl.library\n");
  219.         return FALSE;
  220.     }
  221.     if((DosBase = OpenLibrary("dos.library",37)) == NULL)
  222.     {
  223.         printf("Error: could not open dos.library\n");
  224.         return FALSE;
  225.     }
  226.     if((IFFBase = OpenLibrary("iff.library",23)) == NULL)
  227.     {
  228.         printf("Error: could not open iff.library\n");
  229.         return FALSE;
  230.     }
  231.     if((RexxSysBase = OpenLibrary("rexxsyslib.library",35)) == NULL)
  232.     {
  233.         printf("Error: could not open rexxsyslib.library\n");
  234.         return FALSE;
  235.     }
  236.     if((IconBase = OpenLibrary("icon.library",37)) == NULL)
  237.     {
  238.         printf("Error: could not open icon.library\n");
  239.         return FALSE;
  240.     }
  241.   return TRUE;
  242. }
  243.  
  244. /* Close any open library */
  245. static void closeLibs()
  246. {
  247.     if(IconBase)
  248.         CloseLibrary(IconBase);
  249.     if(RexxSysBase)
  250.         CloseLibrary(RexxSysBase);
  251.     if(IFFBase)
  252.         CloseLibrary(IFFBase);
  253.     if(DosBase)
  254.         CloseLibrary(DosBase);
  255.     if(AslBase)
  256.         CloseLibrary(AslBase);
  257.     if(GadToolsBase)
  258.         CloseLibrary(GadToolsBase);
  259.     if(IntuitionBase)
  260.         CloseLibrary(IntuitionBase);
  261.     if(GfxBase)
  262.         CloseLibrary(GfxBase);
  263. }
  264.